﻿using DataStruct;
using SMARTQuery;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management;
using System.Text;
using System.Threading.Tasks;
using Utils;

namespace MyDiskInfo.Service
{
    /// <summary>
    /// 硬盘信息工具类
    /// </summary>
    public class DiskInfoUtil
    {
        private string _fullPath = null;

        public DiskModel2 GetInfo()
        {
            DiskModel2 result = new DiskModel2();

            try
            {
                //查询硬盘
                if (_fullPath == null)
                {
                    ManagementClass mc = new ManagementClass("Win32_DiskDrive");
                    ManagementObjectCollection moc = mc.GetInstances();
                    foreach (ManagementObject mo in moc)
                    {
                        _fullPath = mo["Name"].ToString() + " " + mo["Caption"].ToString();
                        LogUtil.Log(string.Format("_fullPath={0}", _fullPath));
                        break;
                    }
                }

                //查询硬盘SMART信息
                byte[] deviceInfoBytes = new byte[4096];
                byte[] smartInfoBytes = new byte[512];
                List<string[]> smartInfoList = new List<string[]>();
                SMARTQueryUtil smartQueryUtil = new SMARTQueryUtil();
                smartQueryUtil.getDeviceAndSmartData(_fullPath, ref deviceInfoBytes, ref smartInfoBytes);
                smartQueryUtil.parseSmartInformation_NVMe(smartInfoBytes, ref smartInfoList);

                //取SMART信息属性值
                Dictionary<string, string> dictSmartInfo = smartInfoList.ToLookup(a => a[1]).ToDictionary(a => a.Key, a => a.First()[2]);

                result.DataRead = (int)Math.Round(Convert.ToInt32(dictSmartInfo["Data Units Read"]) / 2.0 / 1024.0);
                result.DataWritten = (int)Math.Round(Convert.ToInt32(dictSmartInfo["Data Units Written"]) / 2.0 / 1024.0);
                result.PercentageUse = Convert.ToInt32(dictSmartInfo["Percentage Use"]);
                result.Temperature = Convert.ToInt32(dictSmartInfo["Composite Temperature"]);

                string[] strArr = ConfigUtil.LBATodayWrite.Split('|');
                string strToday = DateTime.Now.ToString("yyyyMMdd");
                if (strToday == strArr[0])
                {
                    int lastNum = Convert.ToInt32(strArr[1]);
                    result.TodayWritten = result.DataWritten - lastNum;
                }
                else
                {
                    int lastNum = result.DataWritten;
                    result.TodayWritten = 0;
                    ConfigUtil.Save("LBATodayWrite", string.Format("{0}|{1}", strToday, lastNum));
                    LogUtil.Log(string.Format("当天起始数据：{0}|{1}", strToday, lastNum));
                    LogUtil.Log(string.Format("服务端读取固态硬盘信息成功：总写入量：{0}G 剩余寿命：{1}%", result.DataWritten, 100 - result.PercentageUse));
                }
            }
            catch (Exception ex)
            {
                LogUtil.Error(ex, "服务端读取固态硬盘信息失败");
            }

            return result;
        }

    }
}
